/**
 * 
 */
package com.swim.common.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.service.DictDataService;
import com.swim.common.entity.CommonVO;


/**
 * @program     : swim
 * @description : 系统公共service实现层
 * @author      : 刘军武
 * @version     : 1.0  
 * @create      : 2021年3月2日 下午6:47:08
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class CommonService {

	@Autowired
	private DictDataService dictDataService;
	
	/**
	 * 根据“字典类型”、“字典名称”查询对应值
	 * @param dictType
	 * @param treeNames
	 * @return
	 */
	public DictData getDictDataByDictTypeAndTreeNames(String dictType, String treeNames) {
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		dictData.setTreeNames(treeNames);
		List<DictData> dictDataList = dictDataService.findList(dictData);
		if (!CollectionUtils.isEmpty(dictDataList)) {
			dictData = dictDataList.get(0);
		}
		return dictData;
	}
	
	/**
	 * 根据“字典类型”、“字典值”查询对应字典对象
	 * @param dictType
	 * @param dictValue
	 * @return
	 */
	public DictData getDictDataByDictTypeAndDictValue(String dictType, String dictValue) {
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		dictData.setDictValue(dictValue);
		List<DictData> dictDataList = dictDataService.findList(dictData);
		if (!CollectionUtils.isEmpty(dictDataList)) {
			dictData = dictDataList.get(0);
		}
		return dictData;
	}  
	
	/**
	 * 根据“字典类型”、“字典值”查询对应字段名称
	 * @param dictType
	 * @param dictValue
	 * @return dictLabel
	 */
	public String getDictLabelByDictTypeAndDictValue(String dictType, String dictValue) {
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		dictData.setDictValue(dictValue);
		List<DictData> dictDataList = dictDataService.findList(dictData);
		if (!CollectionUtils.isEmpty(dictDataList)) {
			dictData = dictDataList.get(0);
		}
		String dictLabel;
		if (StringUtils.isEmpty(dictData.getDictLabel())) {
			dictLabel = "暂无";
		} else {
			dictLabel = dictData.getDictLabel();
		}
		return dictLabel;
	}  
	
	/**
	 * 根据“字典类型”查询对应字典数据
	 * @param dictType
	 * @return List<CommonVO>
	 */
	public List<CommonVO> getDictDataByDictType(String dictType) {
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		List<DictData> dictDataList = dictDataService.findList(dictData);
		List<CommonVO> commonVOList = new ArrayList<CommonVO>();
		if (!CollectionUtils.isEmpty(dictDataList)) {
			for (DictData itme : dictDataList) {
				CommonVO commonVO = new CommonVO();
				commonVO.setId(itme.getDictValue());
				commonVO.setText(itme.getDictLabel());
				commonVOList.add(commonVO);
			}
		}
		return commonVOList;
	} 
	
	/**
	 * 根据“字典类型”查询对应字典数据
	 * @param dictType
	 * @return Map<String, String> k:DictValue v:DictLabel
	 */
	public Map<String, String> getDictDataMapByDictType(String dictType) {
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		Map<String, String> dictDataMap = new HashMap<String, String>();
		List<DictData> dictDataList = dictDataService.findList(dictData);
		if (!CollectionUtils.isEmpty(dictDataList)) {
			for (DictData itme : dictDataList) {
				dictDataMap.put(itme.getDictValue(), itme.getDictLabel());
			}
		}
		return dictDataMap;
	} 
	
	  /**
	    * CommonVO集合封装成json
	  * @param commonVOList 
	  * @return json
	  * @throws JSONException
	  */
	public String CommonVO2Json(List<CommonVO> commonVOList) throws JSONException {
	     if (commonVOList == null) return "";
	     org.json.JSONArray array = new org.json.JSONArray();
	     JSONObject jsonObject = null;
	     CommonVO commonVO = null;
	     for (int i = 0; i < commonVOList.size(); i++) {
	    	 commonVO = commonVOList.get(i);
	         jsonObject = new JSONObject();
	         jsonObject.put("id", commonVO.getId());
	         jsonObject.put("text", commonVO.getText());
	         array.put(jsonObject);
	     }
	     return array.toString();
    }
	
    /**
     * 将图片转换成Base64编码
     * @param imgFile 待处理图片
     * @return
     */
    public String getImgStr(String imgFile) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(data));
    }
}